package com.cxs.musicplaywidget.commonevent;

import com.cxs.musicplaywidget.data.EventParam;
import com.cxs.musicplaywidget.util.LogUtils;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.aafwk.content.Operation;
import ohos.app.Context;
import ohos.event.commonevent.*;
import ohos.rpc.MessageParcel;
import ohos.rpc.RemoteException;
import ohos.utils.zson.ZSONObject;

import static com.cxs.musicplaywidget.constant.Const.ABILITY_TYPE;
import static com.cxs.musicplaywidget.constant.Const.WIDGET_TYPE;

public class EventManager {
    private static final String tag = EventManager.class.getSimpleName();
    static final String COMMON_EVENT_ACTION = "common_event_action";
    private static boolean  isWidgetSubscribe = false;
    private static boolean  isAbilitySubscribe = false;
    private static Subscriber widgetSubscriber;
    private static Subscriber abilitySubscriber;

    public EventManager() {
        LogUtils.debug(tag, "EventManager()");
    }

    /**
     * Widget订阅
     */
    public static void subscribe(Context context) {
        if (context instanceof AbilitySlice) {
            LogUtils.debug(tag, "subscribe instanceof AbilitySlice");
            abilitySubscribe(context);
        } else if (context instanceof Ability) {
            LogUtils.debug(tag, "subscribe instanceof WidgetAbility");
            widgetSubscribe(context);
        }
    }

    private static void widgetSubscribe(Context context) {
        if (isWidgetSubscribe) {
            return;
        }
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(COMMON_EVENT_ACTION);
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        widgetSubscriber = new Subscriber(context, WIDGET_TYPE, subscribeInfo);
        try {
            CommonEventManager.subscribeCommonEvent(widgetSubscriber);
            isWidgetSubscribe = true;
            LogUtils.debug(tag, "已订阅事件！");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private static void abilitySubscribe(Context context) {
        if (isAbilitySubscribe) {
            return;
        }
        MatchingSkills matchingSkills = new MatchingSkills();
        matchingSkills.addEvent(COMMON_EVENT_ACTION);
        CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills);
        abilitySubscriber = new Subscriber(context, ABILITY_TYPE, subscribeInfo);
        try {
            CommonEventManager.subscribeCommonEvent(abilitySubscriber);
            isAbilitySubscribe = true;
            LogUtils.debug(tag, "已订阅事件！");
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    public static void setIsWidgetSubscribe(boolean isWidgetSubscribe) {
        EventManager.isWidgetSubscribe = isWidgetSubscribe;
    }

    public static void setIsAbilitySubscribe(boolean isAbilitySubscribe) {
        EventManager.isAbilitySubscribe = isAbilitySubscribe;
    }

    public static void unsubscribe(int type) {
        try {
            if (type == WIDGET_TYPE) {
                CommonEventManager.unsubscribeCommonEvent(widgetSubscriber);
                isWidgetSubscribe = false;
            } else {
                CommonEventManager.unsubscribeCommonEvent(abilitySubscriber);
                isAbilitySubscribe = false;
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }


    /**
     * @ClassName EventManager
     * @author cxs
     * @version 1.0
     * @Description 
     * @createTime 2022/3/7
    */ 
    public static void publishEvent(EventParam eventParam) {
        if (eventParam == null) {
            return;
        }
        CommonEventData commonEventData = new CommonEventData(getEventIntent());
        LogUtils.debug(tag, "\"开始发布事件 eventParam\" = " + eventParam);
        commonEventData.setData(ZSONObject.toZSONString(eventParam));
        CommonEventPublishInfo publishInfo = new CommonEventPublishInfo();
        publishInfo.setOrdered(true);
        // 发布事件
        try {
            CommonEventManager.publishCommonEvent(commonEventData, publishInfo);
            LogUtils.debug(tag, "\"发布事件结束 eventParam\" = " + eventParam);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    private static Intent getEventIntent() {
        Intent eventIntent = new Intent();
        Operation operation = new Intent.OperationBuilder()
                .withAction(COMMON_EVENT_ACTION)     // 自定义的事件标识符
                .build();
        eventIntent.setOperation(operation);
        return eventIntent;
    }

}
